// pages/line_detail/line_detail.js
var app, url; 
var headers;
var wxParse=require('../../wxParse/wxParse.js');
var line_id;
var thisflag = true//避免多次重复收藏
Page({
  data:{
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",
    iscollect:false,
    lineId:null,
    scrollimg:[],
    detail:{},
    enableList:[],
    summary_desc:"",
    jour:[],
    cost_include:"",
    cost_exclude:"",
    self_cost:[],
    shopping:[],
    reception_std:"",
    attention_desc:"",
    visa_desc:"",
    spe_tips:"",
    add_desc:""
  },
  jump:function(e){
    var goal=e.currentTarget.dataset.goal;
    var query=wx.createSelectorQuery();
    var top;
    // query.select("#ts").fields({
    //   scrollOffset:true,
    //   properties: ['scrollX', 'scrollY']
    // },function(res){
    //   console.log(res)
    //   }).exec();
    query.select("#" + goal).boundingClientRect()
    query.exec(function(res){
       top = res[0].top
       wx.pageScrollTo({
         scrollTop: top,
       })
      
    })
  },
  collect:function(){
    var that=this;
    var iscollect = this.data.iscollect;
    if (!app.globalData.islogin){ //未登录跳到登录子页面
      wx.navigateTo({
        url: '../toLogin/toLogin',
      })
      return;  
      // wx.setStorageSync("loginUse","../line_detail/line_detail?lineId="+line_id+"");
      // wx.switchTab({
      //   url: '../my/my',
      // })
    }
    if (thisflag){
      thisflag=false
      wx.request({
        url: url + 'api/favor_line',
        method: "POST",
        header: headers,
        data: {
          "line_id": line_id,
          "favorite": iscollect == true ? 0 : 1
        },
        success: function (data) {
          thisflag=true;
          console.log(data)
          if (data.data.result === 1) {
            wx.showModal({
              title: '来自伴行的提示',
              content: data.data.message,
              showCancel: false,
              success: function () {
                that.setData({
                  iscollect: !iscollect
                })
              }
            })
          }
        }
      });
    }
    
    // this.setData({
    //   iscollect:!this.data.iscollect
    // })
  },
  selectdate(e){
    // if (!app.globalData.islogin){//未登录跳转登录子页面暂时不设卡
    //   wx.setStorageSync("loginUse", "../line_detail/line_detail?lineId=" + line_id + "");
    //   wx.switchTab({
    //     url: '../my/my',
    //   });
    //   return;
    // }
    wx.navigateTo({
      url: e.target.dataset.url,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  yuding(){
    wx.navigateTo({
    url: '../../pages/cost_detail/cost_detail',
      // url: '../../pages/test/test',
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  onLoad:function(options){
    line_id = options.lineId;
  },
  onReady:function(options){
    console.log(headers)
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
     app = getApp();
     url = app.globalData.url;
    headers = app.globalData.headers;
    console.log(headers)
    var that = this;
    this.setData({
      lineId: line_id
    });
    // console.log(app)
    // console.log(headers);
    console.log(line_id)
    wx.request({//产品详情
      url: url + 'api/get_line_detail',
      method: "POST",
      header: headers,
      data: {
        "line_id": parseInt(line_id)
      },
      success: function (data) {
        console.log(data);
        let storage = {
          title: data.data.data.title,
          depart: data.data.data.depart,
          back_to: data.data.data.back_to,
          duration: data.data.data.days
        }
        try {
          wx.setStorageSync("costDetailUse", JSON.stringify(storage))
        } catch (e) { }

        let scrollimg = data.data.data.image_list;
        // let enableDate=data.data.data.
        let title = data.data.data.title;
        let jour = [];//行程
        if (data.data.data.schedule){
          for (let j = 0; j < data.data.data.schedule.length; j++) {
            jour.push(data.data.data.schedule[j].journey)//行程
            wxParse.wxParse('jour' + j + '', 'html', data.data.data.schedule[j].journey, that);
            if (j === data.data.data.schedule.length - 1) {
              wxParse.wxParseTemArray("jourArray", 'jour', data.data.data.schedule.length, that)
            }
          }
        } 
        that.setData({
          scrollimg: scrollimg,
          detail: data.data.data,
          summary_desc: data.data.data.summary_desc,
          jour: jour,
          cost_include: data.data.data.cost_include,
          cost_exclude: data.data.data.cost_exclude,
          self_cost: data.data.data.self_cost,
          shopping: data.data.data.shopping,
          reception_std: data.data.data.book_detail.reception_std,
          attention_desc: data.data.data.book_detail.attention_desc,
          visa_desc: data.data.data.book_detail.visa_desc,
          spe_tips: data.data.data.book_detail.spe_tips,
          add_desc: data.data.data.book_detail.add_desc,
          iscollect: data.data.data.favorite === 0 ? false : true
        })
        wxParse.wxParse('summary_desc', 'html', data.data.data.summary_desc, that, 5);//简述
        wxParse.wxParse('cost_include', 'html', data.data.data.cost_include, that, 5);
        wxParse.wxParse('cost_exclude', 'html', data.data.data.cost_exclude, that, 5);
        wxParse.wxParse('reception_std', 'html', data.data.data.book_detail.reception_std, that, 5);
        wxParse.wxParse('attention_desc', 'html', data.data.data.book_detail.attention_desc, that, 5);
        wxParse.wxParse('visa_desc', 'html', data.data.data.book_detail.visa_desc, that, 5);
        wxParse.wxParse('spe_tips', 'html', data.data.data.book_detail.spe_tips, that, 5);
        wxParse.wxParse('add_desc', 'html', data.data.data.book_detail.add_desc, that, 5);
      }
    });
    wx.request({//可选团期
      url: url + 'api/get_enable_team',
      method: "POST",
      header: headers,
      data: {
        line_id: line_id
      },
      success: function (data) {
        console.log(data);
        var enableList = [];
        if (data.data.data.enable_team_list){
          for (var i = 0; i < data.data.data.enable_team_list.length; i++) {
            if (i >= 3) {
              break;
            }
            enableList[i] = data.data.data.enable_team_list[i]
            enableList[i].time_subday = enableList[i].time_day.substring(5)
            that.setData({
              enableList: enableList
            })
            try {
              wx.setStorageSync('enable_team_list', JSON.stringify(data.data.data.enable_team_list))
            } catch (e) {
            }

          }
        }
        
        
      }
    });
    console.log(this.data)
  },
  onShareAppMessage:function(res){
    var that=this;
    res.from ="menu";
    return {
      title: that.data.detail.title,
      path: "/pages/line_detail/line_detail?lineId=" +line_id+"",
      imageUrl:"https://app1.sztravel.com.cn/file/weixinapp/img/shareUrl.jpg",
      success:function(req){
        console.log(req)
      }
    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})